package health.lm.com.component_base.popup.uplevel;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;

import java.util.ArrayList;
import java.util.List;

import health.lm.com.component_base.R;
import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.component_base.popup.sign.SignPopBean;
import health.lm.com.component_base.widget.CustomPopWindow;
import health.lm.com.component_base.popup.model.PopupModel;
import health.lm.com.network.callback.SimpleCallBack;

/**
 * Created by Android Studio .
 * 作者：zhong
 * 日期：2018/12/27
 * 邮箱：15206394364@163.com
 * 介绍：
 * 修订：====================
 */
public class UpLevelPopUtil {
    public static UpLevelPopUtil upLevelPopUtil;
    private static Context ctx;
    private CustomPopWindow popWindow;
    private RelativeLayout rl_main;

    private UpLevelPopUtil() {
    }

    public static UpLevelPopUtil getInstance(){
        if (null == upLevelPopUtil) upLevelPopUtil = new UpLevelPopUtil();
        return upLevelPopUtil;
    }

    public void init(Context context, View mLayoutPopup) {
        init(context, mLayoutPopup, "", new ArrayList<>());
    }

    /**
     * 显示popwindow
     */
    public void init(Context context, View mLayoutPopup, String id, List<SignPopBean> beanList) {
        ctx = context;
        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_up_level, null);
        //主布局
        rl_main = contentView.findViewById(R.id.rl_main);
        TextView tvTitle = contentView.findViewById(R.id.tv_title);
        TextView tvContent = contentView.findViewById(R.id.tv_content);
        TextView btnOk = contentView.findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(v -> popWindow.dissmiss());
        btnOk.setText("收下了");
        ImageView ivTop = contentView.findViewById(R.id.sign_title);

        ImageView imageView = contentView.findViewById(R.id.imageView);
        YoYo.with(Techniques.FadeIn)
                .duration(700)
                .repeat(YoYo.INFINITE)
                .pivot(YoYo.CENTER_PIVOT, YoYo.CENTER_PIVOT)
                .interpolate(new AccelerateDecelerateInterpolator())
                .playOn(imageView);

////        RequestOptions options = new RequestOptions()
////                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
////        Glide.with(ctx).load(R.mipmap.float_congratulation_bg_light2).apply(options).into(imageView);
//        Animation animation = AnimationUtils.loadAnimation(ctx, R.anim.rotate);
//        animation.setInterpolator(new LinearInterpolator());
//        animation.setDetachWallpaper(false);
//        imageView.startAnimation(animation);
        PopupModel.getInstance().PopUpLevel(new SimpleCallBack<UpLevelBean>() {
            @Override
            public void onSuccess(UpLevelBean pUpLevelBean) {
                //status 是否升级了 0否 1是
                if (pUpLevelBean.getStatus().equals("0")) return;
                tvTitle.setText(pUpLevelBean.getUp_title());
                tvContent.setText(pUpLevelBean.getUp_tip());
                ImageLoaderHelper.getInstance().load(context,pUpLevelBean.getUp_img(),ivTop);
                //创建并显示popWindow显示大小
                popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                        .setView(contentView)
                        .enableBackgroundDark(true)
                        .setBgDarkAlpha(0.3f)
                        .setAnimationStyle(R.style.pop_bottom_anim)
                        .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)//显示大小
                        .create()
                        .showAtLocation(mLayoutPopup, Gravity.CENTER, 0, 0);
                //PlayAudioHelper.getInstance().play(context,PlayAudioHelper.GUZHANG);
            }
        });
    }
}
